
#include "DemoContrastAndBrightnessWidget.h"
#include "ui_DemoContrastAndBrightnessWidget.h"

DemoContrastAndBrightnessWidget::DemoContrastAndBrightnessWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DemoContrastAndBrightnessWidget)
{
    ui->setupUi(this);
}

DemoContrastAndBrightnessWidget::~DemoContrastAndBrightnessWidget()
{
    delete ui;
}

void DemoContrastAndBrightnessWidget::setFilePath(QString filePath)
{
    _filePath = filePath;
    ui->lineEdit_filePath->setText(_filePath);
}

void DemoContrastAndBrightnessWidget::updateInfo()
{
    if(_srcMat.data == 0)
    {
        return;
    }
    // mat行列与图片高度是对角线反向的
    ui->label_size->setText(QString("%1 x %2").arg(_srcMat.rows).arg(_srcMat.cols));
    ui->label_channels->setText(QString("%1").arg(_srcMat.channels()));
    ui->label_depth->setText(QString("%1").arg(_srcMat.depth()));
    ui->label_type->setText(QString("%1").arg(_srcMat.type()));
}

void DemoContrastAndBrightnessWidget::updateImage()
{
    if(_srcImage.isNull())
    {
        return;
    }

    cv::Mat srcMat = image2Mat(_srcImage);

    // 增强对比度
    float r;
    float g;
    float b;
    _dstMat = cv::Mat::zeros(srcMat.size(), srcMat.type());
    int alpha = ui->horizontalSlider_alpha->value();    // 小于1，则降低对比度
    int beta = ui->horizontalSlider_beta->value();     // 负数，则降低亮度
    for(int row = 0; row < srcMat.rows; row++)
    {
        for(int col = 0; col < srcMat.cols; col++)
        {
            b = srcMat.at<cv::Vec3b>(row, col)[0];
            g = srcMat.at<cv::Vec3b>(row, col)[1];
            r = srcMat.at<cv::Vec3b>(row, col)[2];
            // 对比度、亮度计算公式 cv::saturate_cast<uchar>(value)：防止溢出
            _dstMat.at<cv::Vec3b>(row, col)[0] = cv::saturate_cast<uchar>(b * alpha / 100.0f + beta);
            _dstMat.at<cv::Vec3b>(row, col)[1] = cv::saturate_cast<uchar>(g * alpha / 100.0f + beta);
            _dstMat.at<cv::Vec3b>(row, col)[2] = cv::saturate_cast<uchar>(r * alpha / 100.0f + beta);
        }
    }
    _dstImage = mat2Image(_dstMat);
    ui->widget_image->setImage(_dstImage);
}

void DemoContrastAndBrightnessWidget::on_pushButton_openFile_clicked()
{
    if(!_srcImage.load(_filePath))
    {
        qDebug() << __FILE__ << __LINE__ << "Failed to load image:" << _filePath;
        return;
    }
    qDebug() << __FILE__<< __LINE__ << (int)_srcImage.format();
    _srcMat = image2Mat(_srcImage);
    updateInfo();
    updateImage();
}

void DemoContrastAndBrightnessWidget::on_horizontalSlider_beta_sliderMoved(int position)
{
    ui->spinBox_beta->setValue(position);
    updateImage();
}

void DemoContrastAndBrightnessWidget::on_horizontalSlider_alpha_sliderMoved(int position)
{
    ui->doubleSpinBox_alpha->setValue(position / 100.0f);
    updateImage();
}

void DemoContrastAndBrightnessWidget::on_pushButton_broswer_clicked()
{
    QString dir = ui->lineEdit_filePath->text();
    dir = dir.mid(0, dir.lastIndexOf("/"));
    QString filePath = QFileDialog::getOpenFileName(0, "打开图片", dir, "PNG;JPEG;BMP(*.png;*.jpg;*.bmp);;JPEG(*.jpg);;PNG(*.png);;BMP(*.bmp)");
    if(filePath.isEmpty())
    {
        return;
    }
    _filePath = filePath;
    ui->lineEdit_filePath->setText(_filePath);

}

void DemoContrastAndBrightnessWidget::on_spinBox_beta_valueChanged(int arg1)
{
    ui->horizontalSlider_beta->setValue(arg1);
    updateImage();
}

void DemoContrastAndBrightnessWidget::on_doubleSpinBox_alpha_valueChanged(double arg1)
{
    ui->horizontalSlider_alpha->setValue(arg1 * 100);
    updateImage();
}

void DemoContrastAndBrightnessWidget::on_pushButton_showFile_clicked()
{
    if(_dstMat.data == 0)
    {
        QMessageBox::information(this, "提示", "使用opencv显示图片失败");
        return;
    }
    cv::imshow("showFile", _dstMat);
}

void DemoContrastAndBrightnessWidget::on_pushButton_backgroundColor_clicked()
{
    QColor backgroundColor = ui->widget_image->getBackgroundColor();
    backgroundColor = QColorDialog::getColor(backgroundColor, this, "底色");
    if(!backgroundColor.isValid())
    {
       return;
    }
    QColor color(backgroundColor.red(), backgroundColor.green(), backgroundColor.blue());
    ui->widget_image->setBackgroundColor(color);
}

